package com.happy.cat.project.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.happy.cat.common.utils.StringUtils;
import com.happy.cat.common.utils.poi.ExcelUtil;
import com.happy.cat.framework.web.controller.BaseController;
import com.happy.cat.framework.web.domain.AjaxResult;
import com.happy.cat.framework.web.page.TableDataInfo;
import com.happy.cat.framework.aspectj.lang.annotation.Log;
import com.happy.cat.framework.aspectj.lang.enums.BusinessType;
import com.happy.cat.project.system.domain.CatGiftCase;
import com.happy.cat.project.system.service.ICatGiftCaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 礼物宝箱Controller
 *
 * @author verne
 * @date 2020-07-18
 */
@AllArgsConstructor
@RestController
@RequestMapping("/system/giftCase" )
@Api("礼物宝箱 API")
public class CatGiftCaseController extends BaseController {

    private final ICatGiftCaseService iCatGiftCaseService;

    /**
     * 查询礼物宝箱列表
     */
    @ApiOperation(value = "查询礼物宝箱列表",notes = "查询礼物宝箱列表",response = TableDataInfo.class)
    @PreAuthorize("@ss.hasPermi('system:giftCase:list')")
    @GetMapping("/list")
    public TableDataInfo list(CatGiftCase catGiftCase)
    {
        startPage();
        LambdaQueryWrapper<CatGiftCase> lqw = new LambdaQueryWrapper<CatGiftCase>();
        if (StringUtils.isNotBlank(catGiftCase.getCaseName())){
            lqw.like(CatGiftCase::getCaseName ,catGiftCase.getCaseName());
        }
        if (StringUtils.isNotBlank(catGiftCase.getCaseImg())){
            lqw.eq(CatGiftCase::getCaseImg ,catGiftCase.getCaseImg());
        }
        if (catGiftCase.getCaseMoney() != null){
            lqw.eq(CatGiftCase::getCaseMoney ,catGiftCase.getCaseMoney());
        }
        if (catGiftCase.getCreateDate() != null){
            lqw.eq(CatGiftCase::getCreateDate ,catGiftCase.getCreateDate());
        }
        if (catGiftCase.getStatus() != null){
            lqw.eq(CatGiftCase::getStatus ,catGiftCase.getStatus());
        }
        List<CatGiftCase> list = iCatGiftCaseService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出礼物宝箱列表
     */
    @ApiOperation(value = "导出礼物宝箱列表",notes = "导出礼物宝箱列表",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:giftCase:export')" )
    @Log(title = "礼物宝箱" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(CatGiftCase catGiftCase) {
        LambdaQueryWrapper<CatGiftCase> lqw = new LambdaQueryWrapper<CatGiftCase>(catGiftCase);
        List<CatGiftCase> list = iCatGiftCaseService.list(lqw);
        ExcelUtil<CatGiftCase> util = new ExcelUtil<CatGiftCase>(CatGiftCase. class);
        return util.exportExcel(list, "giftCase" );
    }

    /**
     * 获取礼物宝箱详细信息
     */
    @ApiOperation(value = "获取礼物宝箱详细信息",notes = "获取礼物宝箱详细信息",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:giftCase:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iCatGiftCaseService.getById(id));
    }

    /**
     * 新增礼物宝箱
     */
    @ApiOperation(value = "新增礼物宝箱",notes = "新增礼物宝箱",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:giftCase:add')" )
    @Log(title = "礼物宝箱" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CatGiftCase catGiftCase) {
        return toAjax(iCatGiftCaseService.save(catGiftCase) ? 1 : 0);
    }

    /**
     * 修改礼物宝箱
     */
    @ApiOperation(value = "修改礼物宝箱",notes = "修改礼物宝箱",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:giftCase:edit')" )
    @Log(title = "礼物宝箱" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CatGiftCase catGiftCase) {
        return toAjax(iCatGiftCaseService.updateById(catGiftCase) ? 1 : 0);
    }

    /**
     * 删除礼物宝箱
     */
    @ApiOperation(value = "删除礼物宝箱",notes = "删除礼物宝箱",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:giftCase:remove')" )
    @Log(title = "礼物宝箱" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iCatGiftCaseService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
